package com.ilia.leek.controller;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.ilia.leek.common.enums.ResultCode;
import com.ilia.leek.common.result.ResultResponse;
import com.ilia.leek.entity.pojo.QueryUser;
import com.ilia.leek.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @author Alice on 2021/4/16
 * @version 1.0
 * @since 1.0
 */
@Slf4j
@RestController
@RequestMapping("/user")
@Api(tags = "用户模块")
@ApiSupport(author = "ilia")
public class UserController {

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    /**
     * 全平台登录(支持code/用户名/手机号登录
     *
     * @param queryUser loginKey登录key
     *                  password 密码
     * @return JSONObject
     */
    @PostMapping("login")
    @Operation(summary = "全平台登录", description = "支持code/用户名/手机号登录,可以自动判断")
    @ApiOperationSupport(
            includeParameters = {"queryUser.loginKey", "queryUser.password"})
    public ResultResponse<Object> doLogin(@RequestBody QueryUser queryUser) {
        if (ObjectUtil.hasEmpty(queryUser.getLoginKey(), queryUser.getPassword())) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        return userService.doLogin(queryUser.getLoginKey(), queryUser.getPassword());
    }

    /**
     * 更新自己
     * (目前允许更改性别,昵称,头像)
     *
     * @param queryUser 用户
     * @return {@link ResultResponse<Object>}
     */
    @PostMapping("updateMyself")
    @Operation(summary = "更新自己", description = "目前允许更改性别,昵称,头像")
    @ApiOperationSupport(
            ignoreParameters = {"loginKey", "oldPassword", "password"})
    public ResultResponse<Object> updateMyself(@RequestBody QueryUser queryUser) {
        if (ObjectUtil.isEmpty(queryUser)) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        return userService.updateMyself(queryUser);
    }

    @PostMapping("updatePassword")
    @Operation(summary = "更新密码")
    @ApiOperationSupport(
            includeParameters = {"queryUser.oldPassword", "queryUser.password"})
    public ResultResponse<Object> updatePassword(@RequestBody QueryUser queryUser) {
        if (ObjectUtil.hasEmpty(queryUser, queryUser.getOldPassword(), queryUser.getPassword())) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        return userService.updateMyself(queryUser);
    }

    /**
     * 刷新token
     *
     * @return {@link ResultResponse<Object>}
     */
    @GetMapping("updateLogin")
    @Operation(summary = "刷新token")
    public ResultResponse<Object> updateLogin() {
        try {
            StpUtil.checkActivityTimeout();
            StpUtil.updateLastActivityToNow();
        } catch (NotLoginException e) {
            if (NotLoginException.TOKEN_TIMEOUT.equals(e.getType())) {
                log.error("登录已经超时,不允许续期.");
            }
            return ResultResponse.failed();
        }
        return ResultResponse.success(StpUtil.getTokenInfo());
    }


    /**
     * 验证是否登录
     *
     * @return {@link ResultResponse<Boolean>}
     */
    @GetMapping("isLogin")
    @Operation(summary = "是否登录")
    public ResultResponse<Boolean> isLogin() {
        return ResultResponse.success(StpUtil.isLogin());
    }


    /**
     * 注销登录
     *
     * @return {@link ResultResponse<Boolean>}
     */
    @GetMapping("logout")
    @Operation(summary = "注销登录")
    public ResultResponse<Boolean> logout() {
        StpUtil.logout();
        return ResultResponse.success();
    }
}
